package voice.main;

import sound.SoundRecorder;
import thread.NotifyingThread;
import thread.SpeechToTextTask;
import thread.ThreadCompleteListener;
import util.TaskHelper;
import adapter.ConversationArrayAdapter;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class VoiceChatFragment extends ChatFragment implements OnClickListener, ThreadCompleteListener  {  

    private TextView speakStatusText;
    private Dialog dialog;
    private boolean recordCancel;
	private SoundRecorder recorder;
    private NotifyingThread recordThread;
    private Handler mHandler;
    private VoiceChatFragment voiceChatFragment;
    
    public VoiceChatFragment() { 
    }  

    @Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    	voiceChatFragment = this;
    	rootView = inflater.inflate(R.layout.voice_chat, container, false);  
    	adapter = new ConversationArrayAdapter(getActivity().getApplicationContext(), R.layout.list_item_conversation);
    	((ListView) rootView.findViewById(R.id.conversationListView)).setAdapter(adapter);  
    	
    	ImageButton b = (ImageButton) rootView.findViewById(R.id.commandButton);
        b.setOnClickListener(this);
        
        mHandler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
              String text = (String)msg.obj;
              if (text == "EndSoundProcess") 
              {
              	dialog.dismiss();
              	if (!recordCancel) 
              		new SpeechToTextTask(voiceChatFragment).execute("Records/sample.wav");
              }
              else speakStatusText.setText(text);
          }
      };
      recorder = new SoundRecorder(getActivity().getApplicationContext(), mHandler);
        return rootView;  
    }  
    

    public void onClick(View v) {
		switch (v.getId()) {
		    case R.id.commandButton:
		    	if (!TaskHelper.isNetworkConnected(getActivity().getApplicationContext())) 
		    	{
		    		Toast.makeText(getActivity().getApplicationContext(), "No internet connection is found.", Toast.LENGTH_SHORT).show();
		    		return;
		    	}
		    	dialog = new Dialog(this.getActivity());
				dialog.setContentView(R.layout.speak_dialog);
				dialog.setTitle("Speak something !");
		
		        speakStatusText = (TextView) dialog.findViewById(R.id.speakStatus);
		        recordCancel = false;
				
				Button dialogButtonDone = (Button) dialog.findViewById(R.id.dialogButtonDone);
				dialogButtonDone.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						recordThread.doStop();
						dialog.dismiss();
					}
				});
				
				Button dialogButtonCancel = (Button) dialog.findViewById(R.id.dialogButtonCancel);
				dialogButtonCancel.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						recordThread.doCancel();
						dialog.dismiss();
						recordCancel = true;
					}
				});
		 
				dialog.show();
				
				recordThread = new NotifyingThread() {
		    		public void doRun() {
		    	        recorder.start();
		    		}
		    		public void doStop() {
		    			recorder.stop();
		    		}
					public void doCancel() {
						recorder.cancel();
						
					}
				};
				recordThread.addListener(this);
				recordThread.start();
		    	
		        break;
	    }
	}

    
    public void notifyOfThreadComplete(Thread thread) {
		Message msg = new Message();
		msg.obj = "EndSoundProcess";
		mHandler.sendMessage(msg);
	}
} 
